package com.example.test_01.service;

import static android.content.ContentValues.TAG;

import android.util.Log;

import com.example.test_01.model.User;
import com.example.test_01.utils.Result;

import org.litepal.LitePal;

import java.util.List;

/**
 * @Desc  用户相关服务实现
 */
public class UserServiceImpl implements UserService{
    /**
     * @param user 用户
     * @return  登录成功与否
     */
    public Result login(User user){
        boolean isExited = LitePal.isExist(User.class,"username = ? and password = ?",user.getUsername(),user.getPassword());
        if(isExited)
            return Result.SUCCESS;
        else
            return Result.ERROR;
    }

    /**
     * @param user
     * @return 注册
     */
    public Result regist(User user){
        user.save();
        return Result.SUCCESS;
    }

    @Override
    public List<User> list() {
        List<User> userList = LitePal.findAll(User.class);
        for(int i = 0; i < userList.size();i++){
            Log.d(TAG, "list: User信息" + userList.get(i).toString());
        }
        return userList;
    }

    @Override
    public Result delUserById(int id) {
        LitePal.delete(User.class,id);
        return Result.SUCCESS;
    }

    @Override
    public User getUserById(int id) {
        return LitePal.find(User.class,id);
    }

    @Override
    public List<User> getUserByUserNameAndPwd(String username, String pwd) {
        return LitePal.where("username = ? and password = ?",username,pwd).find(User.class);
    }
}
